Micron Document
`:top
A `!user-defined function`! (`!UDF`!) is a `F33f`_`[function`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_(programming)]`_`f provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment. UDFs are usually written for the requirement of its creator.

>>Contents

• `F0af`_`[BASIC language`#basic-language]`_`f
• `F0af`_`[COBOL language`#cobol-language]`_`f
• `F0af`_`[Databases`#databases]`_`f
• `F0af`_`[SQL Server 2000`#sql-server-2000]`_`f
• `F0af`_`[Apache Hive`#apache-hive]`_`f
• `F0af`_`[Apache Doris`#apache-doris]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>BASIC language

In some old implementations of the `F33f`_`[BASIC`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=BASIC]`_`f programming language, user-defined functions are defined using the "DEF FN" syntax. More modern dialects of BASIC are influenced by the `F33f`_`[structured programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Structured_programming]`_`f paradigm, where most or all of the code is written as user-defined functions or procedures, and the concept becomes practically redundant.

>>COBOL language

In the `F33f`_`[COBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=COBOL]`_`f programming language, a user-defined function is an entity that is defined by the user by specifying a FUNCTION-ID paragraph. A user-defined function must return a value by specifying the RETURNING phrase of the procedure division header and they are invoked using the function-identifier syntax. See the ISO/IEC 1989:2014 Programming Language COBOL standard for details.

As of May 2022, the IBM Enterprise COBOL for `F33f`_`[z/OS`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Z/OS]`_`f 6.4 (`F33f`_`[IBM COBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=IBM_COBOL]`_`f) compiler contains support for user-defined functions.

>>Databases

In `F33f`_`[relational database management systems`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Relational_database_management_system]`_`f, a user-defined function provides a mechanism for extending the functionality of the `F33f`_`[database server`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Database_server]`_`f by adding a function, that can be evaluated in standard `F33f`_`[query language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Query_language]`_`f (usually `F33f`_`[SQL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SQL]`_`f) statements. The `F33f`_`[SQL standard`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISO/IEC_9075]`_`f distinguishes between `F33f`_`[scalar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scalar_(computing)]`_`f and table functions. A scalar function returns only a single value (or `F33f`_`[NULL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Null_(SQL)]`_`f), whereas a table function returns a (relational) table comprising zero or more rows, each row with one or more columns.

User-defined functions in SQL are declared using the `B100`F9d9CREATE FUNCTION`f`b statement. For example, a user-defined function that converts Celsius to Fahrenheit (a temperature scale used in USA) might be declared like this:

`B100`F9d9CREATE FUNCTION dbo.CtoF(Celsius FLOAT)`f`b
`B100`F9d9 RETURNS FLOAT`f`b
`B100`F9d9 RETURN (Celsius * 1.8) + 32`f`b

Once created, a user-defined function may be used in `F33f`_`[expressions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Expression_(programming)]`_`f in SQL statements. For example, it can be invoked where most other intrinsic functions are allowed. This also includes `F33f`_`[SELECT statements`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Select_(SQL)]`_`f, where the function can be used against data stored in tables in the database. Conceptually, the function is evaluated once per row in such usage. For example, assume a table named `B100`F9d9Elements`f`b, with a row for each known chemical element. The table has a column named BoilingPoint for the boiling point of that element, in Celsius. The query

`B100`F9d9SELECT Name, CtoF(BoilingPoint)`f`b
`B100`F9d9 FROM Elements`f`b

would retrieve the name and the boiling point from each row. It invokes the `B100`F9d9CtoF`f`b user-defined function as declared above in order to convert the value in the column to a value in Fahrenheit.

Each user-defined function carries certain properties or characteristics. The SQL standard defines the following properties:

• Language - defines the programming language in which the user-defined function is implemented; examples include SQL, C, C# and Java.
• Parameter style - defines the conventions that are used to pass the function parameters and results between the implementation of the function and the database system (only applicable if language is not SQL).
• Specific name - a name for the function that is unique within the database. Note that the function name does not have to be unique, considering `F33f`_`[overloaded functions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Overloaded_function]`_`f. Some SQL implementations require that function names are unique within a database, and overloaded functions are not allowed.
• Determinism - specifies whether the function is deterministic or not. The determinism characteristic has an influence on the `F33f`_`[query optimizer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Query_optimizer]`_`f when compiling a SQL statement.
• SQL-data access - tells the database management system whether the function contains no SQL statements (NO SQL), contains SQL statements but does not access any tables or `F33f`_`[views`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=View_(SQL)]`_`f (CONTAINS SQL), reads data from tables or views (READS SQL DATA), or actually modifies data in the database (MODIFIES SQL DATA).

User-defined functions should not be confused with `F33f`_`[stored procedures`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stored_procedure]`_`f. Stored procedures allow the user to group a set of SQL commands. A procedure can accept parameters and execute its SQL statements depending on those parameters. A procedure is not an expression and, thus, cannot be used like user-defined functions.

Some database management systems allow the creation of user defined functions in languages other than SQL. `F33f`_`[Microsoft SQL Server`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_SQL_Server]`_`f, for example, allows the user to use `F33f`_`[.NET languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=List_of_CLI_languages]`_`f including C# for this purpose. DB2 and Oracle support user-defined functions written in C or Java programming languages.

>>>SQL Server 2000

There are three types of UDF in `F33f`_`[Microsoft SQL Server`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_SQL_Server]`_`f 2000: `F33f`_`[scalar functions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scalar_function]`_`f, inline table-valued functions, and multistatement table-valued functions.

Scalar functions return a single data value (not a table) with RETURNS clause. Scalar functions can use all scalar data types, with exception of timestamp and user-defined data types. Inline table-valued functions return the `F33f`_`[result set`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Result_set]`_`f of a single SELECT statement. Multistatement table-valued functions return a table, which was built with many TRANSACT-SQL statements.

User-defined functions can be invoked from a query like built‑in functions such as OBJECT_ID, LEN, DATEDIFF, or can be executed through an EXECUTE statement like stored procedures.

Performance Notes: 1. On Microsoft SQL Server 2000 a table-valued function which "wraps" a View may be much faster than the View itself. The following MyFunction is an example of a "function-wrapper" which runs faster than the underlying view MyView:

`B100`F9d9CREATE FUNCTION MyFunction()`f`b
`B100`F9d9 RETURNS @Tbl TABLE`f`b
`B100`F9d9 (`f`b
`B100`F9d9 StudentID VARCHAR(255),`f`b
`B100`F9d9 SAS_StudentInstancesID INT,`f`b
`B100`F9d9 Label VARCHAR(255),`f`b
`B100`F9d9 Value MONEY,`f`b
`B100`F9d9 CMN_PersonsID INT`f`b
`B100`F9d9 )`f`b
`B100`F9d9AS`f`b
`B100`F9d9BEGIN`f`b
`B100`F9d9 INSERT @Tbl`f`b
`B100`F9d9 (`f`b
`B100`F9d9 StudentID,`f`b
`B100`F9d9 SAS_StudentInstancesID,`f`b
`B100`F9d9 Label,`f`b
`B100`F9d9 Value,`f`b
`B100`F9d9 CMN_PersonsID`f`b
`B100`F9d9 )`f`b
`B100`F9d9 SELECT`f`b
`B100`F9d9 StudentID,`f`b
`B100`F9d9 SAS_StudentInstancesID,`f`b
`B100`F9d9 Label,`f`b
`B100`F9d9 Value,`f`b
`B100`F9d9 CMN_PersonsID`f`b
`B100`F9d9 FROM MyView -- where MyView selects (with joins) the same columns from large table(s)`f`b
`B100`F9d9`f`b
`B100`F9d9 RETURN`f`b
`B100`F9d9END`f`b

2. On Microsoft SQL Server 2005 the result of the same code execution is the opposite: view is executed faster than the "function-wrapper".

User-defined functions are subroutines made of one or more Transact-SQL statements that can be used to encapsulate code for reuse. It takes zero or more arguments and evaluates a return value. Has both control-flow and DML statements in its body similar to stored procedures. Does not allow changes to any Global Session State, like modifications to database or external resource, such as a file or network. Does not support output parameter. DEFAULT keyword must be specified to pass the default value of parameter. Errors in UDF cause UDF to abort which, in turn, aborts the statement that invoked the UDF.

`B100`F9d9CREATE FUNCTION CubicVolume`f`b
`B100`F9d9-- Input dimensions in centimeters`f`b
`B100`F9d9(`f`b
`B100`F9d9 @CubeLength decimal(4,1),`f`b
`B100`F9d9 @CubeWidth decimal(4,1),`f`b
`B100`F9d9 @CubeHeight decimal(4,1)`f`b
`B100`F9d9)`f`b
`B100`F9d9 RETURNS decimal(12,3)`f`b
`B100`F9d9AS`f`b
`B100`F9d9BEGIN`f`b
`B100`F9d9 RETURN(@CubeLength * @CubeWidth * @CubeHeight)`f`b
`B100`F9d9END`f`b

Data type supported in Microsoft SQL Server 2000 Like a temporary table used to store results Mostly used to define `F33f`_`[temporary variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Temporary_variable]`_`f of type (table) and the return value of a UDF The scope is limited to function, stored procedure, or batch in which it is defined Assignment operation is not allowed between (Table) variables May be used in SELECT, INSERT, UPDATE, and DELETE CREATE FUNCTION to create UDF ALTER FUNCTION to change the characteristics of UDF DROP FUNCTION to remove UDF

>>>Apache Hive

`F33f`_`[Apache Hive`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Apache_Hive]`_`f defines, in addition to the regular user-defined functions (UDF), also user-defined aggregate functions (UDAF) and table-generating functions (UDTF).`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] Hive enables developers to create their own custom functions with Java.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]

>>>Apache Doris

Apache Doris, an open-source real-time analytical database, allows external users to contribute their own UDFs written in C++ to it.`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f "LanguageManual UDF - Apache Hive - Apache Software Foundation". 26 June 2015.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "HivePlugins - Apache Hive - Apache Software Foundation". 26 June 2015.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "Apache Doris UDF". Retrieved 8 April 2023.

>>External links

• Microsoft SQL Server reference for CREATE FUNCTION
• MySQL manual section on UDFs
• DB2 CREATE FUNCTION statement

`c`F0af`_`[↑ Back to top`#top]`_`f`a